import SwiftUI

struct ChoiceLanguageView: View {
    @Binding var selectedLanguage: String
    @Binding var isPresented: Bool

    var body: some View {
        ZStack {
            MainView()
            VStack{
              
                HStack {
                    Spacer()
                    Text("Choose language")
                        .font(.title)
                        .fontWeight(.bold)
                        .foregroundColor(.white)
                    Spacer()
                    Button(action: {
                        isPresented = false
                    }) {
                        Text("Done")
                            .foregroundColor(.blue)
                            .padding(.trailing, 20)
                    }
                }
                .padding(.top, 30)

                List(languages, id: \.self) { language in
                    LanguageRow(language: language, isSelected: language == selectedLanguage)
                        .contentShape(Rectangle())
                        .onTapGesture {
                            selectedLanguage = language
                        }
                        .listRowBackground(Color("mainback")) // 设置每一行的背景色
                        .foregroundColor(Color.white) // 设置字体颜色
                }
                
                
                .listStyle(PlainListStyle())

                Spacer()
            }
            
         
        }        .navigationViewStyle(StackNavigationViewStyle())

    }
}

struct LanguageRow: View {
    let language: String
    let isSelected: Bool

    var body: some View {
        VStack {
            HStack {
                Text(language)
                Spacer()
                if isSelected {
                    Image(systemName: "checkmark")
                        .foregroundColor(.blue)
                }
            }
            .padding(.vertical, 10)
          
        }
    }
}

struct ChoiceLanguageView_Previews: PreviewProvider {
    @State static var selectedLanguage = "English"
    @State static var isPresented = true

    static var previews: some View {
        ChoiceLanguageView(selectedLanguage: $selectedLanguage, isPresented: $isPresented)
    }
}


struct ChoiceLanguageViews: View {
    @Binding var selectedLanguage: String
    @Binding var isPresented: Bool
    var onSelectLanguage: () -> Void // 添加一个闭包属性

    var body: some View {
        VStack {
            HStack {
                Spacer()
                Text("Choose language")
                    .font(.title)
                    .fontWeight(.bold)
                Spacer()
                Button(action: {
                    isPresented = false
                }) {
                    Text("Done")
                        .foregroundColor(.blue)
                        .padding(.trailing, 20)
                }
            }
            .padding(.top, 30)

            List(languages, id: \.self) { language in
                LanguageRow(language: language, isSelected: language == selectedLanguage)
                    .contentShape(Rectangle())
                    .onTapGesture {
                        selectedLanguage = language
                        // 当用户选择语言后执行闭包
                        onSelectLanguage()
                    }
            }
            .listStyle(PlainListStyle())

            Spacer()
        }
    }
}

